package No0003_longest_substring_without_repeating_characters

import "fmt"

func lengthOfLongestSubstring(s string) int {
	var ans int = 0
	l := len(s)
	var left = 0
	var m map[byte]int
	m = make(map[byte]int)
	for i := 0; i < l; i++ {
		if containsKeyInMap(m, s[i]) {
			left = max(left, m[s[i]]+1)
		}
		m[s[i]] = i
		ans = max(ans, i-left+1)
	}
	return ans
}

func containsKeyInMap(m map[byte]int, key byte) bool {
	_, ret := m[key]
	return ret
}

func max(x, y int) int {
	if x > y {
		return x
	} else {
		return y
	}
}

var solution func(string) int = lengthOfLongestSubstring

func Test() {
	ret := solution("pwwkew")
	fmt.Println(ret)
}
